#include<graphics.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
int rp[3][3] = { 0 };
int y[9] = { 0 };
IMAGE img[9];
SYSTEMTIME start;
MOUSEMSG m;
int mx = 0;
int my = 0;
void imageload()
{
	LoadImage(&img[0], _T("image1.jpg"));
	LoadImage(&img[1], _T("image2.jpg"));
	LoadImage(&img[2], _T("image3.jpg"));
	LoadImage(&img[3], _T("image4.jpg"));
	LoadImage(&img[4], _T("image5.jpg"));
	LoadImage(&img[5], _T("image6.jpg"));
	LoadImage(&img[6], _T("image7.jpg"));
	LoadImage(&img[7], _T("image8.jpg"));
	LoadImage(&img[8], _T("image9.jpg"));
}

void initimage()
{
	int n = 0;
	for (int y = 0; y < 3; y++)
		for (int x = 0; x < 3; x++)
			putimage(x * 100, y * 100,&img[0]);
	        n++;
}
void begin()
{

}
void miximage()
{
	int y;
	int x;
	srand((int)time(0));
	for (int y = 0; y < 3; y++)
		for (int x = 0; x < 3; x++)
			rp[y][x] = rand() % 9;
	        putimage(100 * x, 100 * y, &img[rp[y][x]]);
}

void control()
{
	mx = getx() / 100;
	my = gety() / 100;
	char c;
	int k;
	if (_kbhit)
	{
		c = _getch();
		switch (c) {
		case 'w':
			if ((my / 100) > 0)
			{
				rp[my][mx] = k;
				rp[my][mx] = rp[my - 1][mx];
				rp[my - 1][mx] = k;
				putimage(mx * 100, (my - 1) * 100, &img[rp[my - 1][mx]]);
				putimage(mx * 100, 100 * my, &img[rp[my][mx]]);
				my--;
			}
		case 's':
			if ((my / 100) < 2)
			{
				rp[my][mx] = k;
				rp[my][mx] = rp[my + 1][mx];
				rp[my + 1][mx] = k;
				putimage(mx * 100, (my + 1) * 100, &img[rp[my + 1][mx]]);
				putimage((mx + 1) * 100, 100 * my, &img[rp[my][mx]]);
				my++;
			}
		case 'a':
			if ((mx / 100) > 0)
			{
				rp[my][mx] = k;
				rp[my][mx] = rp[my][mx - 1];
				rp[my][mx - 1] = k;
				putimage((mx - 1) * 100, my * 100, &img[rp[my][mx - 1]]);
				putimage(mx * 100, 100 * my, &img[rp[my][mx]]);
				mx--;
			}
		case 'd':
			if ((mx / 100) < 2)
			{
				rp[my][mx] = k;
				rp[my][mx] = rp[my][mx + 1];
				rp[my][mx + 1] = k;
				putimage(mx * 100, my * 100, &img[rp[my][mx]]);
				putimage((mx + 1) * 100, 100 * my, &img[rp[my][mx + 1]]);
				mx++;
			}

		}
	}
}
/*	RECT r = {0,300,300,400 };
drawtext(_T("Hello World"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
*/
int judge()
{
	int n = 0;
	int k = 0;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j< 3;j++)
			rp[i][j] = y[n];
	        n++;

	for (int v = 0; v < 9; v++)
	{
		if (y[v + 1] < y[v])
			k = 1;
	}

	return k;
}
void time()
{
	SYSTEMTIME now;
	GetLocalTime(&now);
	int d2, h2, m2, s2, d1, h1, m1, s1;
	h2 = now.wHour;
	m2 = now.wMinute;
	s2 = now.wSecond;
	d2 = now.wDay;
	h1 = start.wHour;
	m1 = start.wMinute;
	s1 = start.wSecond;
	d1 = start.wDay;
	RECT r = { 0,300,300,400 };
	drawtext(_T("%d:%d:%d", h2, m2, s2), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	if (d2 >= d1)
		if (h2 >= h1)
			if ((h2 * 3600 + m2 * 60 + s2) - (h1 * 3600 + m1 * 60 + s1) >= 600)
				system("cls");
}
int mian()
{
	initgraph(300, 400);
	setbkcolor(BLACK);
	cleardevice();
	void imageload();
	void imageinit();
	while (1)
	{
		void begin();
		void miximage();
		GetLocalTime(&start);
		void control();
		if (judge())
			system("cls");
		void time();
	}
	return 0;
}
